﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BSC_OTC_Management.BSC_SERVICES;

namespace BSC_OTC_Management.UserControls
{
    public partial class MenuLeft : UserControl
    {
        #region Khai báo biến
        Code.StaticVariable _code = new Code.StaticVariable();
        DataTable _table = new DataTable();
        string _function_name;
        #endregion
        
        public MenuLeft(int _width, int _height)
        {
            InitializeComponent();
            //auto resize
            this.Dock = DockStyle.Fill;
            this.Width = _width;
            this.Height = _height;
            this._table = _code._services.GetMenuLeft();
            GetPermission();
            this.AddTopMenu(this._table);
        }

        private void GetPermission()
        {
            Code.StaticVariable.tablePermission = _code._services.GetMenu_ByGroupsPermission(Code.StaticVariable.groupID);
            for (int i = 0; i < Code.StaticVariable.tablePermission.Rows.Count; i++)
            {
                string _menuID_i = Convert.ToString(Code.StaticVariable.tablePermission.Rows[i]["menuid"]);
                string _authcode_i = Convert.ToString(Code.StaticVariable.tablePermission.Rows[i]["authcode"]);
                for (int j = i + 1; j < Code.StaticVariable.tablePermission.Rows.Count; j++)
                {
                    string _menuID_j = Convert.ToString(Code.StaticVariable.tablePermission.Rows[j]["menuid"]);
                    string _authcode_j = Convert.ToString(Code.StaticVariable.tablePermission.Rows[j]["authcode"]);
                    if (_menuID_i == _menuID_j)
                    {
                        // nếu trùng nhau tức là user này thuộc nhiều hơn 1 nhóm quyền giống nhau, xoa 1 cái đi
                        if (_authcode_i == _authcode_j)
                        {
                            Code.StaticVariable.tablePermission.Rows.RemoveAt(j);
                        }
                        // nếu khác nhau, so sánh authcode, ưu tiên lấy giá trị Y
                        else
                        {
                            // gan gia tri moi sau khi so sanh, sau do xoa ban ghi kia di
                            Code.StaticVariable.tablePermission.Rows[i]["authcode"] = this.setAuthCode(_authcode_i, _authcode_j);
                            Code.StaticVariable.tablePermission.Rows.RemoveAt(j);
                        }
                    }
                }
            }
        }

        private string setAuthCode(string _authcode_i, string _authcode_j)
        {
            string result = "";
            for (int i = 0; i < 7; i++)
            {
                if (Convert.ToChar(  _authcode_i.ToCharArray()[i]) == 'Y' || Convert.ToChar( _authcode_j.ToCharArray()[i])=='Y')
                {
                    result += "Y";
                }
                else
                {
                    result += "N";
                }
            }
            return result;
        }

        // Add menu Cha
        private void AddTopMenu(DataTable dt)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "MenuParentID = 0";
            foreach (DataRowView row in dv)
            {
                foreach (DataRow perRow in Code.StaticVariable.tablePermission.Rows)
                {
                    if (perRow["menuid"].ToString() == row["menuid"].ToString())
                    {
                        if (row["MENUCODE"].ToString() == "")
                        {
                             this.tvMenuLeft.Nodes.Add(row["MenuID"].ToString(), row["MenuFullName"].ToString(),1);
                        }
                        else
                        {
                            this.tvMenuLeft.Nodes.Add(row["MenuID"].ToString(), row["MENUCODE"].ToString() + ": " + row["MenuFullName"].ToString(),1);
                        }
                        AddChildMenuItems(dt, this.tvMenuLeft.Nodes[row["MenuID"].ToString()]);
                    }
                }
            }
        }

        // Add cac menu con cấp dưới
        private void AddChildMenuItems(DataTable menuData, TreeNode nodeParent)
        {
            DataView view = new DataView(menuData);
            view.RowFilter = "MenuParentID = " + nodeParent.Name;
            foreach (DataRowView row in view)
            {
                foreach (DataRow perRow in Code.StaticVariable.tablePermission.Rows)
                {
                    if (perRow["menuid"].ToString() == row["menuid"].ToString())
                    {
                        if (row["MENUCODE"].ToString() == "")
                        {
                            nodeParent.Nodes.Add(row["MenuID"].ToString(), row["MenuFullName"].ToString(),1);
                        }
                        else
                        {
                            nodeParent.Nodes.Add(row["MenuID"].ToString(), row["MENUCODE"].ToString() + ": " + row["MenuFullName"].ToString(),1);
                        }
                        AddChildMenuItems(menuData, nodeParent.Nodes[row["MenuID"].ToString()]);
                    }
                }
            }
        }

        private void tvMenuLeft_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.tvMenuLeft.SelectedNode.Nodes.Count == 0)
            {
                DataView dv = new DataView(this._table);
                dv.RowFilter = "MenuID = " + this.tvMenuLeft.SelectedNode.Name;
                string _formTitle = "";
                foreach (DataRowView row in dv)
                {
                    this._function_name = dv[0]["menuname"].ToString();
                    _formTitle = dv[0]["menufullname"].ToString();
                    break;
                }
                this._function_name = dv[0]["menuname"].ToString();
                string _formname = dv[0]["formname"].ToString();
                Code.StaticVariable.formname_Active = _function_name;
                Code.StaticVariable.menuID_Active = this.tvMenuLeft.SelectedNode.Name;
                Code.StaticVariable.menu_Code = dv[0]["menucode"].ToString();
                switch (_formname)
                {
                    case "frmview":
                        Forms.FrmView _FrmView = new Forms.FrmView(_function_name, _formTitle);
                        _FrmView.ShowDialog();
                        break;
                    case "frmphanbolenhmua":
                        Forms.Form1 _Frm_PhanBoLenhKHTC = new Forms.Form1();
                        _Frm_PhanBoLenhKHTC.ShowDialog();
                        break;
                    case "Frm_Group_Permission":
                        Forms.Frm_Group_Permission  _form = new Forms.Frm_Group_Permission();
                        _form.ShowDialog();
                        break;
                    case "frmReport":
                        FrmMainReport _formReport = new FrmMainReport(_function_name, _formTitle);
                        _formReport.ShowDialog();
                        break;
                    case "Frm_SoTraiChu":
                        Forms.SoTraiChu.Frm_SoTraiChu _Frm_SoTraiChu = new Forms.SoTraiChu.Frm_SoTraiChu();
                        _Frm_SoTraiChu.ShowDialog();
                        break;
                    case "Frm_ThanhToanLaiGoc":
                        Forms.SoTraiChu.Frm_ThanhToanLaiGoc _Frm_ThanhToanLaiGoc = new Forms.SoTraiChu.Frm_ThanhToanLaiGoc();
                        _Frm_ThanhToanLaiGoc.ShowDialog();
                        break;
                    case "Frm_ChuyenNhuong":
                        Forms.SoTraiChu.Frm_ChuyenNhuong _Frm_ChuyenNhuong = new Forms.SoTraiChu.Frm_ChuyenNhuong();
                        _Frm_ChuyenNhuong.ShowDialog();
                        break;
                    case "Frm_PhongToa":
                        Forms.SoTraiChu.Frm_PhongToa _Frm_PhongToa = new Forms.SoTraiChu.Frm_PhongToa();
                        _Frm_PhongToa.ShowDialog();
                        break;
                    case "Frm_GiaiPhongToa":
                        Forms.SoTraiChu.Frm_GiaiPhongToa _Frm_GiaiPhongToa = new Forms.SoTraiChu.Frm_GiaiPhongToa();
                        _Frm_GiaiPhongToa.ShowDialog();
                        break;
                    case "Frm_CamCo":
                        Forms.SoTraiChu.Frm_CamCo _Frm_CamCo = new Forms.SoTraiChu.Frm_CamCo();
                        _Frm_CamCo.ShowDialog();
                        break;
                    case "Frm_GiaiCamCo":
                        Forms.SoTraiChu.Frm_GiaiCamCo _Frm_GiaiCamCo = new Forms.SoTraiChu.Frm_GiaiCamCo();
                        _Frm_GiaiCamCo.ShowDialog();
                        break;
                    case "Frm_MuaLai":
                        Forms.SoTraiChu.Frm_MuaLai _Frm_MuaLai = new Forms.SoTraiChu.Frm_MuaLai();
                        _Frm_MuaLai.ShowDialog();
                        break;
                    case "Frm_InSoTraiChu":
                        Forms.SoTraiChu.Frm_InSoTraiChu _Frm_InSoTraiChu = new Forms.SoTraiChu.Frm_InSoTraiChu();
                        _Frm_InSoTraiChu.ShowDialog();
                        break;
                    case "Frm_TraSo":
                        Forms.SoTraiChu.Frm_TraSo _Frm_TraSo = new Forms.SoTraiChu.Frm_TraSo();
                        _Frm_TraSo.ShowDialog();
                        break;
                    case "Frm_InLaiSo":
                        Forms.SoTraiChu.Frm_InLaiSo _Frm_InLaiSo = new Forms.SoTraiChu.Frm_InLaiSo();
                        _Frm_InLaiSo.ShowDialog();
                        break;
                    case "Frm_NopTien":
                        Forms.TKTienKH.Frm_NopTien _Frm_NopTien = new Forms.TKTienKH.Frm_NopTien();
                        _Frm_NopTien.ShowDialog();
                        break;
                    case "Frm_RutTien":
                        Forms.TKTienKH.Frm_RutTien _Frm_RutTien = new Forms.TKTienKH.Frm_RutTien();
                        _Frm_RutTien.ShowDialog();
                        break;
                    case "Frm_PhongToaTien":
                        Forms.TKTienKH.Frm_PhongToaTien _Frm_PhongToaTien = new Forms.TKTienKH.Frm_PhongToaTien();
                        _Frm_PhongToaTien.ShowDialog();
                        break;
                    case "Frm_GiaiToaTien":
                        Forms.TKTienKH.Frm_GiaiToaTien _Frm_GiaiToaTien = new Forms.TKTienKH.Frm_GiaiToaTien();
                        _Frm_GiaiToaTien.ShowDialog();
                        break;
                    case "Frm_UpMauSo":
                        Forms.SoTraiChu.Frm_UpMauSo _Frm_UpMauSo = new Forms.SoTraiChu.Frm_UpMauSo();
                        _Frm_UpMauSo.ShowDialog();
                        break;
                    case "Frm_DuyetTTLai":
                        Forms.SoTraiChu.Frm_DuyetTTLai _Frm_DuyetTTLai = new Forms.SoTraiChu.Frm_DuyetTTLai();
                        _Frm_DuyetTTLai.ShowDialog();
                        break;
                    case "Frm_VanTinKH":
                        Forms.Frm_VanTinKH _Frm_VanTinKH = new Forms.Frm_VanTinKH();
                        _Frm_VanTinKH.ShowDialog();
                        break;
                    case "Frm_TaoSoTraiChuTheoDanhSach":
                        Forms.SoTraiChu.Frm_TaoSoTraiChuTheoDanhSach _Frm_TaoSoTraiChuTheoDanhSach = new Forms.SoTraiChu.Frm_TaoSoTraiChuTheoDanhSach();
                        _Frm_TaoSoTraiChuTheoDanhSach.ShowDialog();
                        break;
                    case "Frm_ImportSignal":
                        Forms.Frm_ImportSignal _Frm_ImportSignal = new Forms.Frm_ImportSignal();
                        _Frm_ImportSignal.ShowDialog();
                        break;
                    default:
                        break;
                }
                this.tvMenuLeft.SelectedNode = null;
            }
        }
    }
}
